/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.misena.praxispraktik.recursos;


import com.misena.praxispraktik.entity.Personas;
import com.misena.praxispraktik.entity.Personas_;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import javax.ws.rs.Consumes;;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

/**
 *
 * @author diana
 */
@Stateless
@Path("/consultarpersona")
public class ConsultarPersona {

    Personas persona;
    EntityManagerFactory emf = null;
    EntityManager em = null;

    @GET
    @Produces("application/json")
    public Personas consultar(@QueryParam("numdoc") String numdoc) {
        //public String consultar(@QueryParam("id") Integer id){
        //public String consultar(){
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            
            //CriteriaQuery<Usuarios> criteria = build.createQuery( Usuarios.class );
             CriteriaQuery criteria =em.getCriteriaBuilder().createQuery();
             Root<Personas> personasRoot = criteria.from( Personas.class );
             criteria.select( personasRoot );
             ParameterExpression<String> numeroIdentificacion = em.getCriteriaBuilder().parameter( String.class );
             criteria.where( em.getCriteriaBuilder().equal( personasRoot.get( Personas_.numdoc ), numeroIdentificacion ) );
             TypedQuery<Personas> query = em.createQuery( criteria );
             query.setParameter( numeroIdentificacion, numdoc );
             persona = query.getSingleResult();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin Consulta");

        //System.out.println(username.getIdUsuarios() + " " + username.getIdPersonas());

        return persona;
    }

    @POST
    @Consumes("application/json")
    //public Response guardar(Personas a) {
    public String guardar(Personas a) {
        String respuesta ="false";
        persona = a;        
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            em.persist(a);
            respuesta = "true";

        } catch (Exception e) {
            e.printStackTrace();
            respuesta ="false";
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin de la inserción");

        //return Response.ok().build();
        return respuesta;
    }

    /**
     *
     * @param a
     * @return
     */
    @PUT
    @Consumes("application/json")
    public Response actualizar(Personas a) {
        persona = a;
        //cliente = p;
        //return Response.ok(p).build();

        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            em.merge(a);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin de la actualización");

        return Response.ok().build();
    }

    
    //@DELETE
    

    public EntityManagerFactory obtenerEntityManagerFactory() {

        return Persistence.createEntityManagerFactory("PraxisPraktikPU");
    }

    public EntityManager obtenerEntityManager(EntityManagerFactory emf) {

        return emf.createEntityManager();
    }
}
